﻿#define _CRT_SECURE_NO_WARNINGS 1
//和为 k 的⼦数组（medium）:https://leetcode.cn/problems/subarray-sum-equals-k/
class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int, int> hash;
        int sum = 0, ret = 0;


        hash[0] = 1;
        for (auto x : nums)
        {
            sum += x;
            if (hash[sum - k])
            {
                ret += hash[sum - k];
            }
            hash[sum]++;
        }
        return ret;
    }
};